package com.qianfeng.chinamobile.strategy.service.impl;

import com.qianfeng.chinamobile.strategy.cache.FilterOrderLocalCache;
import com.qianfeng.chinamobile.strategy.filter.StrategyFilter;
import com.qianfeng.chinamobile.strategy.service.DataFilerManger;
import com.qianfeng.chinamobile.strategy.service.SendReportService;
import com.qianfeng.smsplatform.common.model.Standard_Report;
import com.qianfeng.smsplatform.common.model.Standard_Submit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class DataFilterMangerImpl implements DataFilerManger {

    private SendReportService sendReportService;

    @Autowired
    public void setSendReportService(SendReportService sendReportService) {
        this.sendReportService = sendReportService;
    }

    //    private CacheService cacheService;
//
//    @Autowired
//    public void setCacheService(CacheService cacheService) {
//        this.cacheService = cacheService;
//    }

    private FilterOrderLocalCache localCache;


    @Autowired
    public void setLocalCache(FilterOrderLocalCache localCache) {
        this.localCache = localCache;
    }

    //拿到所有的策略
    private Map<String, StrategyFilter> allFilters;

    @Autowired
    public void setAllFilters(Map<String, StrategyFilter> allFilters) {
        this.allFilters = allFilters;
    }

    @Override
    public void dealSms(Standard_Submit standard_submit) {
        //遍历所有的策略,然后挨个执行
        System.err.println("开始处理消息了");
//
        //   List<String> filerOrderList = cacheService.getDataFromList(CacheConstants.CACHE_FILTER_KEY, 0, -1);
        List<String> filerOrderList = localCache.getFilterOrderList();//从本地缓存中获取所有的过滤器
        if (filerOrderList == null) {
            System.err.println("没有找到过滤器,中断操作");
            return;
        }

        /**
         * 遍历,获取每一个要执行的过滤器的名字,因为这个是有顺序
         */
        for (String fiterName : filerOrderList) {
            StrategyFilter filter = allFilters.get(fiterName);
            if (filter != null) {
                boolean result = filter.delSubmit(standard_submit);
                if (!result) {
                    //拦击了
                    System.err.println(filter.getClass().getSimpleName() + " 阻拦了程序的执行");
                    //我们需要返回结果给客户,并且还需要更新日志
                    standard_submit.setReportState(2);//设置为发送失败

                    Standard_Report standard_report = new Standard_Report();//状态报告的对象,用于通知客户的
                    //通知客户
                    standard_report.setErrorCode(standard_submit.getErrorCode());
                    standard_report.setDescription(standard_submit.getDescription());
                    standard_report.setClientID(standard_submit.getClientID());
                    standard_report.setState(standard_submit.getReportState());
                    standard_report.setSrcID(standard_submit.getSrcNumber());
                    standard_report.setMobile(standard_submit.getDestMobile());
                    standard_report.setSendCount(1);//我们告诉用户结果,万一因为一些原因导致失败,我们要重试,可能会重试很多次,如果多次还是失败就不重试了
                    sendReportService.sendReport(standard_report);

                    //写日志
                    return;//跳出整个操作
                }
            }
        }

    }
}
